package com.ruoyi.gxs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.gxs.domain.GxsAddstudent;
import com.ruoyi.gxs.domain.GxsSchoolrecord;
import com.ruoyi.gxs.domain.GxsYearname;
import com.ruoyi.gxs.service.IGxsSchoolrecordService;
import com.ruoyi.gxs.service.IGxsYearnameService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsScoreAddMapper;
import com.ruoyi.gxs.domain.GxsScoreAdd;
import com.ruoyi.gxs.service.IGxsScoreAddService;

import javax.validation.Validator;

/**
 * 成绩导入Service业务层处理
 * 
 * @author 高晓松
 * @date 2024-03-02
 */
@Service
public class GxsScoreAddServiceImpl implements IGxsScoreAddService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    private GxsScoreAddMapper gxsScoreAddMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    protected Validator validator;

    @Autowired
    private IGxsSchoolrecordService gxsSchoolrecordService;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IGxsYearnameService gxsYearnameService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询成绩导入
     * 
     * @param gxsId 成绩导入主键
     * @return 成绩导入
     */
    @Override
    public GxsScoreAdd selectGxsScoreAddByGxsId(Long gxsId)
    {
        return gxsScoreAddMapper.selectGxsScoreAddByGxsId(gxsId);
    }

    /**
     * 查询成绩导入列表
     * 
     * @param gxsScoreAdd 成绩导入
     * @return 成绩导入
     */
    @Override
    public List<GxsScoreAdd> selectGxsScoreAddList(GxsScoreAdd gxsScoreAdd)
    {
        return gxsScoreAddMapper.selectGxsScoreAddList(gxsScoreAdd);
    }

    /**
     * 新增成绩导入
     * 
     * @param gxsScoreAdd 成绩导入
     * @return 结果
     */
    @Override
    public int insertGxsScoreAdd(GxsScoreAdd gxsScoreAdd)
    {
        return gxsScoreAddMapper.insertGxsScoreAdd(gxsScoreAdd);
    }

    /**
     * 修改成绩导入
     * 
     * @param gxsScoreAdd 成绩导入
     * @return 结果
     */
    @Override
    public int updateGxsScoreAdd(GxsScoreAdd gxsScoreAdd)
    {
        return gxsScoreAddMapper.updateGxsScoreAdd(gxsScoreAdd);
    }

    /**
     * 批量删除成绩导入
     * 
     * @param gxsIds 需要删除的成绩导入主键
     * @return 结果
     */
    @Override
    public int deleteGxsScoreAddByGxsIds(Long[] gxsIds)
    {
        return gxsScoreAddMapper.deleteGxsScoreAddByGxsIds(gxsIds);
    }

    /**
     * 删除成绩导入信息
     * 
     * @param gxsId 成绩导入主键
     * @return 结果
     */
    @Override
    public int deleteGxsScoreAddByGxsId(Long gxsId)
    {
        return gxsScoreAddMapper.deleteGxsScoreAddByGxsId(gxsId);
    }

    @Override
    public String importScoreAdd(List<GxsScoreAdd> gxsScoreAddList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(gxsScoreAddList) || gxsScoreAddList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }

        // 定义变量
        int i , j;
        int successNum = 0;
        int failureNum = 0;
        int subject = 0;
        float sum = 0;
        float average = 0;
        String result = "";
        boolean flage = true;

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        GxsSchoolrecord gxsSchoolrecord = new GxsSchoolrecord();

        //循环判断每一条数据
        for ( i = 0; i < gxsScoreAddList.size(); i++)
        {
            //判断班级是否存在
            boolean className = sysDeptService.selectStudentByUserName(gxsScoreAddList.get(i).getClassName());

            if (className){
                GxsYearname gxsYearname = gxsYearnameService.selectYearnameByYearname(gxsScoreAddList.get(i).getSchoolYear());
                if (!StringUtils.isNull(gxsYearname)){
                        SysUser user = sysUserService.selectUserByUserName(gxsScoreAddList.get(i).getUserName());
                    if (!StringUtils.isNull(user)){
                        //初始化 gxsSchoolrecord 学号
                        gxsSchoolrecord.setUserName(gxsScoreAddList.get(i).getUserName());

                        //把成绩转换成float格式
                        result = gxsScoreAddList.get(i).getResult();
                        //判断是不是最后一条数据，最后一条数据比较对象为自身
                        if (i + 1 < gxsScoreAddList.size())
                            //如果不是最后一条数据，则跟其下一条比较
                            j = i + 1;

                        else {
                            j = i;
                            //如果是最后一条数据，则把标志位改为 false
                            flage = false;
                        }

                        //比较两条数据的学号是不是一样
                        boolean equals = gxsScoreAddList.get(i).getUserName().equals(gxsScoreAddList.get(j).getUserName());


                        gxsSchoolrecord.setUserId(user.getUserId());
                        //查询班级
                        SysDept dept = sysDeptMapper.selectDeptByCalssName(gxsScoreAddList.get(i).getClassName());
                        gxsSchoolrecord.setDeptId(dept.getDeptId());
                        //查看学年
                        gxsSchoolrecord.setGxsYear(gxsYearname.getGxsYearid());

                        //对条件进行或运算
                        if ( equals && flage) {
                            //如果学号一样，则加科目成绩，学科也进行+1
                            sum += Float.valueOf(result);
                            subject ++;
                            //判断需不需要补考
                            if (gxsScoreAddList.get(i).getRemark().equals("需补考")){
                                gxsSchoolrecord.setGxsFail(1L);
                            }
                            else {
                                gxsSchoolrecord.setGxsFail(0L);
                            }
                        }
                        else {
                            //如果学号不一样则表示两个人，将平均分进行计算，其余值初始化
                            sum += Float.valueOf(result);
                            subject ++;
                            average = sum / subject;

                            //判断需不需要补考
                            if (gxsScoreAddList.get(i).getRemark().equals("需补考")){
                                gxsSchoolrecord.setGxsFail(1L);
                            }

                            //初始化 gxsSchoolrecord 成绩
                            gxsSchoolrecord.setGxsSchoolrecord(average);
                            gxsSchoolrecordService.insertGxsSchoolrecord(gxsSchoolrecord);

                            sum = average = subject = 0;
                            flage = true;
                        }

                        //把数组中的第i条加入到数据库中
                        gxsScoreAddMapper.insertGxsScoreAdd(gxsScoreAddList.get(i));
                        successNum ++ ;
                    }
                    else
                    {
                        failureNum ++;
                        String msg = "<br/>" + gxsScoreAddList.get(i).getUserName() + "  学生不存在 " ;
                        failureMsg.append(msg );
                    }
                }
                else
                {
                    failureNum ++;
                    String msg = "<br/>" + gxsScoreAddList.get(i).getSchoolYear() + "  学年不存在 " ;
                    failureMsg.append(msg );
                }
            }
            else
            {
                failureNum ++;
                String msg = "<br/>" + gxsScoreAddList.get(i).getClassName() + "  班级不存在 " ;
                failureMsg.append(msg  );
            }
        }



        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
